package com.yc.services;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yc.DTO.DiaryDTO;
import com.yc.bean.Diary;
import com.yc.dao.DiaryDao;
import com.yc.web.controller.model.ResponseResult;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class DiaryServiceImpl implements DiaryService {

    @Autowired
    private DiaryDao diaryDao;

    @Override
    public ResponseResult createDiary(DiaryDTO diaryDTO) {
        Diary diary = new Diary();
        BeanUtils.copyProperties(diaryDTO, diary);

        // 设置默认值
        diary.setCreateTime(LocalDateTime.now());
        diary.setUpdateTime(LocalDateTime.now());
        diary.setStatus(1); // 默认状态为正常
        int result = diaryDao.insert(diary);
        if (result > 0) {
            return ResponseResult.ok("日记创建成功").setData(diary.getDiaryId());
        }
        return ResponseResult.error("日记创建失败");
    }

    @Override
    public ResponseResult updateDiary(DiaryDTO diaryDTO) {
        Diary diary = new Diary();
        BeanUtils.copyProperties(diaryDTO, diary);

        // 只更新必要的字段
        diary.setUpdateTime(LocalDateTime.now());

        int result = diaryDao.updateById(diary);
        if (result > 0) {
            return ResponseResult.ok("日记更新成功");
        }
        return ResponseResult.error("日记更新失败");
    }

    @Override
    public ResponseResult deleteDiary(Long diaryId, Long userId) {
        Diary diary = new Diary();
        diary.setDiaryId(diaryId);
        diary.setStatus(0); // 设置为删除状态
        diary.setUpdateTime(LocalDateTime.now());

        // 确保只能删除自己的日记
        QueryWrapper<Diary> wrapper = new QueryWrapper<>();
        wrapper.eq("diaryId", diaryId).eq("userId", userId);

        int result = diaryDao.update(diary, wrapper);
        if (result > 0) {
            return ResponseResult.ok("日记删除成功");
        }
        return ResponseResult.error("日记删除失败");
    }

    @Override
    public ResponseResult getDiaryList(Long userId, Integer pageNum, Integer pageSize) {
        QueryWrapper<Diary> wrapper = new QueryWrapper<>();
        wrapper.eq("userId", userId)
                .eq("status", 1) // 只查询正常的日记
                .orderByDesc("updateTime");

        Page<Diary> page = new Page<>(pageNum, pageSize);
        IPage<Diary> diaryPage = diaryDao.selectPage(page, wrapper);

        // 转换为DTO
        List<DiaryDTO> diaryDTOs = diaryPage.getRecords().stream().map(diary -> {
            DiaryDTO dto = new DiaryDTO();
            BeanUtils.copyProperties(diary, dto);
            return dto;
        }).collect(Collectors.toList());

        return ResponseResult.ok().setData(diaryDTOs);
    }

    @Override
    public ResponseResult getDiaryDetail(Long diaryId, Long userId) {
        QueryWrapper<Diary> wrapper = new QueryWrapper<>();
        wrapper.eq("diaryId", diaryId)
                .eq("userId", userId)
                .eq("status", 1);

        Diary diary = diaryDao.selectOne(wrapper);
        if (diary != null) {
            DiaryDTO diaryDTO = new DiaryDTO();
            BeanUtils.copyProperties(diary, diaryDTO);
            return ResponseResult.ok().setData(diaryDTO);
        }
        return ResponseResult.error("日记不存在或已被删除");
    }

    // 在 DiaryServiceImpl 类中添加以下方法
    @Override
    public ResponseResult searchDiaryByTitle(Long userId, String title, Integer pageNum, Integer pageSize) {
        QueryWrapper<Diary> wrapper = new QueryWrapper<>();
        wrapper.eq("userId", userId)
                .eq("status", 1) // 只查询正常的日记
                .like("title", title) // 模糊查询标题
                .orderByDesc("updateTime");

        Page<Diary> page = new Page<>(pageNum, pageSize);
        IPage<Diary> diaryPage = diaryDao.selectPage(page, wrapper);

        // 转换为DTO
        List<DiaryDTO> diaryDTOs = diaryPage.getRecords().stream().map(diary -> {
            DiaryDTO dto = new DiaryDTO();
            BeanUtils.copyProperties(diary, dto);
            return dto;
        }).collect(Collectors.toList());

        // 返回分页信息
        return ResponseResult.ok("搜索完成").setData(Map.of(
                "records", diaryDTOs,
                "total", diaryPage.getTotal(),
                "current", diaryPage.getCurrent(),
                "size", diaryPage.getSize(),
                "pages", diaryPage.getPages()
        ));
    }
}